#pragma once

#include <iostream>
#include <cstdio>
#include <string>
#include <fstream>
#include <sstream>
#include <memory>
#include <filesystem> //C++17
#include <unistd.h>
#include <time.h>
#include "Mutex.hpp"

namespace LogModule
{
    using namespace LockMoudle;
    //获取当前系统时间
    std::string CurrentTime()
    {
        time_t time_stamp = ::time(nullptr);
        struct tm curr;
        localtime_r(&time_stamp, &curr); //时间戳, 获取可读性较强的时间信息
        char buffer[1024];
        snprintf(buffer, sizeof(buffer), "%4d-%02d-%02d %02d:%02d:%02d",
            curr.tm_year + 1900,
            curr.tm_mon + 1,
            curr.tm_mday,
            curr.tm_hour,
            curr.tm_min,
            curr.tm_sec);
        return buffer;
    }

    //构成: 1. 构建日志字符串 2.刷新落盘
    //落盘策略(screen, file)
    //1.日志文件的默认路径和文件名
    const std::string defaultlogpath = "./log/";
    const std::string defaultlogname = "log.txt";

    //2.日志等级
    enum class LogLevel
    {
        DEBUG = 1,
        INFO,
        WARNING,
        ERROR,
        FATAL
    };

    //枚举类型转字符串
    std::string Level2String(LogLevel level)
    {
        switch (level)
        {
        case LogLevel::DEBUG:
            return "DEBUG";
        case LogLevel::INFO:
            return "INFO";
        case LogLevel::WARNING:
            return "WARNING";
        case LogLevel::ERROR:
            return "ERROR";
        case LogLevel::FATAL:
            return "FATAL";
        default:
            return "None";
        }
    }

    //3.刷新策略
    class LogStrategy
    {
    public:
        virtual ~LogStrategy() = default; //虚析构函数,多态,能够正确调用对象进行析构, 编译器自动生成
        virtual void SyncLog(const std::string &message) = 0;//纯虚函数,子类必须手动实现
    };
    //3.1控制台策略
    class ConsoleLogStrategy : public LogStrategy
    {
    public:
        ConsoleLogStrategy()
        {}
        ~ConsoleLogStrategy()
        {}
        //向控制台打印日志信息message
        void SyncLog(const std::string &message)
        {
            LockGuard lockguard(_lock);
            std::cout << message << std::endl;
        }
    private:
        Mutex _lock;
    };
    //文件级策略
    class FileLogStrategy : public LogStrategy
    {
    public:
        FileLogStrategy(const std::string &logpath = defaultlogpath, const std::string &logname = defaultlogname)
        : _logpath(logpath),
          _logname(logname)
        {
            //确定_logpath是否存在
            LockGuard lockguard(_lock);
            if(std::filesystem::exists(_logpath))
            {
                return ;
            }
            try
            {
                //不存在进行创建
                std::filesystem::create_directories(_logpath);
            }
            catch(std::filesystem::filesystem_error &e)
            {
                std::cerr << e.what() << "\n";
            }
        }
        ~FileLogStrategy(){}
        //重写基类虚方法
        void SyncLog(const std::string &message)
        {
            LockGuard lockguard(_lock);
            std::string log = _logpath + _logname;
            std::ofstream out(log, std::ios::app);
            if(!out.is_open()) {return;}
            out << message << "\n";
            out.close(); 
        }
    private:
        std::string _logpath;
        std::string _logname;

        Mutex _lock;
    };
    //日志类: 构建日志字符串, 根据策略, 进行写入
    class Logger
    {
    public:
        Logger()
        {
            //默认采用控制台策略
            _strategy = std::make_shared<ConsoleLogStrategy>();
        }
        void EnableConsolelog()
        {
            _strategy = std::make_shared<ConsoleLogStrategy>();
        }
        void EnableFileLog()
        {
            _strategy = std::make_shared<FileLogStrategy>();
        }
        
        ~Logger(){}
        //一条完整的信息[2024-08-04 12:27:03] [DEBUG] [202938] [main.cc] [16] + 日志的可变部分(<< "hello world" << 3.14 << a << b;)
        //外部类实现策略, 内部类实现信息
        class LogMessage
        {
        public:
        LogMessage(LogLevel level, const std::string &filename, int line, Logger &logger)
        : _currtime(CurrentTime()),
          _level(level),
          _pid(::getpid()),
          _filename(filename),
          _line(line),
          _logger(logger)
        {
            std::stringstream ssbuffer;
            ssbuffer << "[" << _currtime << "] "
                    << "[" << Level2String(_level) << "] "
                    << "[" << _pid << "] "
                    << "[" << _filename << "] "
                    << "[" << _line << "] - ";
            _loginfo = ssbuffer.str();
        }
        template <typename T>
        LogMessage &operator<<(const T &info)
        {
            std::stringstream ss;
            ss << info;
            _loginfo += ss.str();
            return *this;
        }

        ~LogMessage()
        {
            if (_logger._strategy)
            {
                _logger._strategy->SyncLog(_loginfo);
            }
        }

        private:
            std::string _currtime; //当前日志的时间
            LogLevel _level; //日志等级
            pid_t _pid; //进程pid
            std::string _filename; //源文件
            int _line; //行号
            Logger &_logger; //策略
            std::string _loginfo; //日志信息
        };  
    //重载operator(), 故意的拷贝
    LogMessage operator()(LogLevel level, const std::string &filename, int line)
    {
        return LogMessage(level, filename, line, *this);
    }
    private:
        std::shared_ptr<LogStrategy> _strategy;
    };
    Logger logger;

    #define LOG(Level) logger(Level, __FILE__, __LINE__)
    #define ENABLE_CONSOLE_LOG() logger.EnableConsolelog()
    #define ENABLE_FILE_LOG() logger.EnableFileLog()
}


